$(document).ready(function(){
    $('#tabs').tabs();
    $("#dtvalidade").datepicker({
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        dayNamesMin: ['Dom','Seg','Ter','Qua','Qui','Sex','Sab'],
        dateFormat: 'dd/mm/yy'
    });
    $('#arquiv').uploadify({
        'uploader'  : '/js/lib/uploadify/uploadify.swf',
        'script'    : '/js/lib/uploadify/uploadify.php',
        'cancelImg' : '/js/lib/uploadify/cancel.png',
        'auto'      : true,
        'folder'    : '/uploads',
        'multi'     : true,
        onComplete  : completou
    });
    function completou(e,q,file,response){
        var aux = new String;
        if ($('#files').val()==''){
            aux = escape(file.name);
        }else{
            aux = $('#files').val()+','+escape(file.name);
        }
        $('#arquivo').val(aux);
        $('#files').val(aux);
    };
     $('.arquivos').click(function(){
       var aux = $(this).attr('arq');
       window.open('/propostas/arquivo/id/'+aux,'Anexo....');
       return false;
    });
    /*$('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url : '/js/tiny_mce/tiny_mce.js',

        // General options
        theme : "advanced",
        plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

        // Theme options
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor",
        theme_advanced_buttons2 : '',
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,

        // Example content CSS (should be your site CSS)
        content_css : "css/content.css",

        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "lists/template_list.js",
        external_link_list_url : "lists/link_list.js",
        external_image_list_url : "lists/image_list.js",
        media_external_list_url : "lists/media_list.js",

        // Replace values for the template plugin
        template_replace_values : {
            username : "Some User",
            staffid : "991234"
        }
    });*/
    $('#accolaborador').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "/colaborador/index",
                type: "POST",
                datatype: "json",
                data: {
                    q: request.term
                },
                success: function(data) {
                    response($.map(data.colaboradores, function(item) {
                        return {
                            label: item.nome,
                            value: item.codusu
                        }
                    }))
                }
            })
        },
        focus: function(event,ui){
            $('#accolaborador').val(ui.item.label);
            return false;
        },
        open: function(event,ui) {
            $('#colaborador_id').val('');
        },
        select: function(event,ui){
            $('#colaborador_id').val(ui.item.value);
            $('#accolaborador').val(ui.item.label);
            if ($('#colaborador_id').val()!=''){
                $.ajax({
                    url: "/contato/index",
                    processData: false,
                    data: 'cliente_id='+$('#colaborador_id').val(),
                    beforeSend: function (){
                        var pos = $('#accolaborador').position();
                        var width = $('#accolaborador').width();
                        $("#ajax-ldr").css( {
                            "left": (pos.left+width+5) + "px",
                            "top":pos.top + "px"
                        } ).fadeIn('fast');

                    },
                    success: function(msg){
                        $('#contato_id option').remove();
                        $('#contato_id').append("<option value=''>Selecione o Contato...</option>")
                        for (i = 0; i < msg.length; i++) {
                            $('#contato_id').append("<option value='"+msg[i].idcontato+"'>"+msg[i].dsnome+"</option>")
                        }
                        $('#ajax-ldr').fadeOut('fast');
                    }
                });
            }
            return false;
        },
        minLength: 3,
        delay: 300
    });
});
